package com.cc.service.iml;

import com.cc.constants.EasyBuyEnum;
import com.cc.mapper.OrderMapper;
import com.cc.model.EbOrder;
import com.cc.model.EbProduct;
import com.cc.service.OrderService;
import com.cc.service.ProductService;
import com.cc.vo.OrderProductVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Random;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private ProductService productService;
    @Autowired
    private OrderMapper orderMapper;
    @Override
    public List<EbOrder> list(Integer page, Integer pageSize) {
        return orderMapper.findByPage((page-1)*pageSize,pageSize);
    }

    @Override
    public int save(EbOrder ebOrder) {
        return orderMapper.save(ebOrder);
    }

    @Override
    public int getMaxPage(int pageSize) {
        return orderMapper.getMaxPage(pageSize);
    }

    @Override
    public List<EbOrder> listes(String eo_user_id, String eo_user_name, Integer page, Integer pageSize) {
        return orderMapper.findByIdAndName(eo_user_id,eo_user_name,page,pageSize);
    }


    @Override
    public int deleteByPk(int eoId) {
        return orderMapper.delete(eoId);
    }

    @Override
    public int getMaxPages(String eo_user_id,String eo_user_name, int pageSize) {
        return orderMapper.getMaxPagesById(eo_user_id,eo_user_name,pageSize);
    }

    @Override
    public EbOrder selectByCondition(int eoId) {
        return orderMapper.findById(eoId);
    }

    @Override
    public int updateById(EbOrder ebOrder) {

        int sss  = orderMapper.updateByPrimaryKey(ebOrder);

        return sss;

    }

    public EbOrder createOrder(OrderProductVo orderProductVo) {

        //1.判断库存
        for(EbProduct ebProduct:orderProductVo.getProductList()){
            EbProduct detail = productService.selectByCondition(ebProduct.getEp_id());
            if(detail.getEp_stock()<ebProduct.getCount()){
                throw new RuntimeException("库存不足，请重新下单！");
            }
        }
        //2.创建订单并添加订单
        //String orderId = UUID.randomUUID().toString();
        //2.1 创建订单号
        Integer orderId = new Random().nextInt(1000)+1000000;
        //2.2 创建订单
        EbOrder ebOrder = new EbOrder();
        ebOrder.setEo_id(orderId);
        BigDecimal totalMoney = BigDecimal.ZERO;
        for(EbProduct ebProduct:orderProductVo.getProductList()){
            totalMoney.add(ebProduct.getEp_price().multiply(new BigDecimal(ebProduct.getCount())));
        }

        ebOrder.setEo_cost(100);
        ebOrder.setEo_create_time("2000-01-11");
        ebOrder.setEo_status(EasyBuyEnum.OrderStatus.ORDER.getCode());
        //添加发货地址地址
        ebOrder.setEo_user_address("天马上冻");
        //添加用户id
        ebOrder.setEo_user_id("123");
        //添加用户姓名
        ebOrder.setEo_user_name("二狗");

        //添加类型   默认在线支付
        ebOrder.setEo_type(EasyBuyEnum.OrderType.ONLINE.getCode());
        //

        orderMapper.save(ebOrder);
        //3.创建订单详情

        //4.减库存

        //5.跳转到支付
        return ebOrder;
    }




}
